package net.sareweb.acab.actions.request;

import java.io.Serializable;
import java.util.Date;

import javax.faces.event.ActionEvent;

import net.sareweb.acab.components.util.RequestCreationData;
import net.sareweb.acab.entity.Request;
import net.sareweb.acab.entity.manager.RequestManager;
import net.sareweb.acab.entity.manager.UserManager;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.remoting.WebRemote;
import org.jboss.seam.core.Events;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Credentials;

@Name("createRequestBean")
@Scope(ScopeType.CONVERSATION)
public class CreateRequestBean implements Serializable{
	@Logger
	private Log log;
	
	@Out
	RequestCreationData requestCreationData;
	
	@In(create=true)
	UserManager userManager;
	@In(create=true)
	RequestManager requestManager;
	@In Credentials credentials;
	
	
	
		
	
	
	public String chooseLocationTypeAction(){	
		log.debug("the way to choose location: {0}", requestCreationData.getLocationType());
		if(requestCreationData.getLocationType()==null)return "";
		else if(requestCreationData.getLocationType().equals("city"))return "cityLocation";
		else if(requestCreationData.getLocationType().equals("maps"))return "gMapsLocation";
		else return "";
	}
	
	
	public String chooseCoordsAction(){
		log.debug("(the way to choose location: {0})", requestCreationData.getLocationType());
		log.debug("from coords: {0},{1} to coords: {2},{3}", requestCreationData.getFromLat(), requestCreationData.getFromLon(), requestCreationData.getToLat(), requestCreationData.getToLon());
		return "choosePeriodType";
	}
	
	public String choosePeriodTypeAction(){
		log.debug("the way to choose period: {0}", requestCreationData.getPeriodType());
		return requestCreationData.getPeriodType();
	}
	
	public String selectSingleDateAction(){
		log.debug("selected date for request: {0}", requestCreationData.getRequestDate1());
		return "summary";
	}
	
	public String selectMultiDateAction(){
		if(requestCreationData.getSelectedItems()!=null){
			if(requestCreationData.getSelectedItems().size()==0){
				log.debug("no week days", "");	
			}
			else{
				for(int i=0; i<requestCreationData.getSelectedItems().size(); i++){
					log.debug("Selected day of week: {0}", requestCreationData.getSelectedItems().get(i));
				}
			}
		}
		else{
			log.debug("no week days", "");
		}
		log.debug("End date for request: {0}", requestCreationData.getRequestDate2());
		log.debug("Begin date for request: {0}", requestCreationData.getRequestDate1());
		log.debug("repType: {0}", requestCreationData.getRepType());

		return "summary";
	}
	
	public String saveRequestAction(){
		return "";
	}
	
	public void saveRequestActionListener(ActionEvent event){
		log.debug("Creating new request ({0})", requestCreationData.getRequestName());
		Request r = new Request();
		r.setName(requestCreationData.getRequestName());
		r.setUser(userManager.findByLoginName(credentials.getUsername()));
		r.setFromLat(requestCreationData.getFromLat().floatValue());
		r.setFromLon(requestCreationData.getFromLon().floatValue());
		r.setToLat(requestCreationData.getToLat().floatValue());
		r.setToLon(requestCreationData.getToLon().floatValue());
		r.setBeginDate(requestCreationData.getRequestDate1());
		r.setEndDate(requestCreationData.getRequestDate2());
		if(requestCreationData.getPeriodType().equals(requestCreationData.PERIODTYPE_SINGLE)){
			r.setSingleDay(true);
		}
		r.setCronExpression("TEST");
		
		r.setCreateDate(new Date());
		
		
		Events.instance().raiseEvent("requestMadeEvent");
		requestManager.save(r);
		
	}
	

	@WebRemote
	public String updateFromPoint(Double lat, Double lng){
		requestCreationData.setFromLat(lat);
		requestCreationData.setFromLon(lng);
		return "(" + requestCreationData.getFromLat() + ","  + requestCreationData.getFromLon() + ")";
	}
	@WebRemote
	public String updateToPoint(Double lat, Double lng){
		requestCreationData.setToLat(lat);
		requestCreationData.setToLon(lng);
		return "(" + requestCreationData.getToLat() + ","  + requestCreationData.getToLon() + ")";
	}
	
	
	
	@Begin
	public String start(){
		return "/acab/request/CreateRequest1.xhtml";
	}
	
	@End
	public String end(){
		return "/home.xhtml";
	}
	
	@Factory(value="requestCreationData")
	public void instantiateCreateData(){
		if(this.requestCreationData==null){
			log.debug("Instantiating creation data container", "");
			this.requestCreationData= new RequestCreationData();
		}
	}
	
	
	


	

	
	
	

}
